
import { post } from "../utils/Client";
import {  Mapper } from "../utils/Map";
import { defineReactive } from "../utils/Observe";

/**材质 */
class Material {
  [x: string]: any;
  guid?: string = undefined;

  index: number = undefined;
  /**3d模型 */
  component: string = undefined;
  /**材质路径 */
  matpath?: string = undefined;
  /**贴图路径 */
  texpath?: string = undefined;
  /**颜色 */
  color?: string = undefined;
  @Mapper("offsetX,offsetY")
  /**材质偏移 */
  offset: {
    offsetX?: number;
    offsetY?: number;
  } = undefined;
  @Mapper("subdivideU,subdivideV")
  /**材质细分 */
  subdivide: {
    /**材质水平细分 */
    subdivideU?: number;
    /**材质垂直细分 */
    subdivideV?: number;
  } = undefined;
  /**粗糙度(0-1) */
  roughness?: number = undefined;
  /**金属度(0-1) */
  metallic?: number = undefined;
  /**高光(0-1) */
  specular?: number = undefined;
  constructor(options) {
    Material.prototype.isMaterial = true;
    Mapper.map(this, options);
  this.index = options.index;
  this.matpath = options.matpath;
  
 

    defineReactive(this, "specular", (value, key) => {
      this.putProperty(key, key);
    });
    defineReactive(this, "matpath", (value, key) => {
      this.putProperty(key, key);
    });
    defineReactive(this, "texpath", (value, key) => {
      this.putProperty(key, key);
    });
    defineReactive(this, "color", (value, key) => {
      this.putProperty(key, key);
    });
    defineReactive(this, "offset", (value, key) => {
      this.putProperty(key, key,true);
    });
    defineReactive(this, "subdivide", (value, key) => {
      this.putProperty(key, key,true);
    });
    defineReactive(this, "roughness", (value, key) => {
      this.putProperty(key, key);
    });
    defineReactive(this, "metallic", (value, key) => {
      this.putProperty(key, key);
    });
    defineReactive(this, "specular", (value, key) => {
      this.putProperty(key, key);
    });
  }

  private putProperty(prop, key,expand?, alias?) {
    console.log("-----",prop, key,expand, alias)
    if (expand) {
      post("object_control_material", {
        ...this[prop],
        guid: this.guid,
        component:this.component,
        index:this.index,
        key:key,
      });
    } else {
      post("object_control_material", {
        [alias || prop]: this[prop],
        guid: this.guid,
        component:this.component,
        index:this.index,
        key:key,

      });
    }
  }

}

export { Material };
